Proper connect_port
[juce-lv2.git] / juce / source / extras / the jucer / src / model / paintelements / jucer_PaintElementText.h
blobae1f40dfffcbfc52d6b2d503b918b1a91c2b7d22
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_PAINTELEMENTTEXT_JUCEHEADER__
27 #define __JUCER_PAINTELEMENTTEXT_JUCEHEADER__
29 #include "jucer_ColouredElement.h"
30 #include "../../properties/jucer_FontPropertyComponent.h"
31 #include "../../properties/jucer_JustificationProperty.h"
34 //==============================================================================
35 /**
37 class PaintElementText : public ColouredElement
39 public:
40 //==============================================================================
41 PaintElementText (PaintRoutine* owner)
42 : ColouredElement (owner, "Text", false, false),
43 text ("Your text goes here"),
44 font (15.0f),
45 typefaceName (FontPropertyComponent::defaultFont),
46 justification (Justification::centred)
48 fillType.colour = Colours::black;
49 position.rect.setWidth (200);
50 position.rect.setHeight (30);
53 ~PaintElementText()
57 //==============================================================================
58 void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
60 fillType.setFillType (g, getDocument(), parentArea);
62 font = FontPropertyComponent::applyNameToFont (typefaceName, font);
63 g.setFont (font);
65 const Rectangle<int> r (position.getRectangle (parentArea, layout));
66 g.drawText (replaceStringTranslations (text, owner->getDocument()),
67 r.getX(), r.getY(), r.getWidth(), r.getHeight(),
68 justification, true);
71 void getEditableProperties (Array <PropertyComponent*>& properties)
73 ColouredElement::getEditableProperties (properties);
75 properties.add (new TextProperty (this));
76 properties.add (new FontNameProperty (this));
77 properties.add (new FontStyleProperty (this));
78 properties.add (new FontSizeProperty (this));
79 properties.add (new TextJustificationProperty (this));
80 properties.add (new TextToPathProperty (this));
83 void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
85 if (! fillType.isInvisible())
87 String r;
89 fillType.fillInGeneratedCode (code, paintMethodCode);
91 String x, y, w, h;
92 positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
94 r << "g.setFont ("
95 << FontPropertyComponent::getCompleteFontCode (font, typefaceName)
96 << ");\ng.drawText ("
97 << quotedString (text)
98 << ",\n "
99 << x << ", " << y << ", " << w << ", " << h
100 << ",\n "
101 << justificationToCode (justification)
102 << ", true);\n\n";
104 paintMethodCode += r;
108 static const char* getTagName() throw() { return "TEXT"; }
110 XmlElement* createXml() const
112 XmlElement* e = new XmlElement (getTagName());
113 position.applyToXml (*e);
114 addColourAttributes (e);
115 e->setAttribute ("text", text);
116 e->setAttribute ("fontname", typefaceName);
117 e->setAttribute ("fontsize", roundToInt (font.getHeight() * 100.0) / 100.0);
118 e->setAttribute ("bold", font.isBold());
119 e->setAttribute ("italic", font.isItalic());
120 e->setAttribute ("justification", justification.getFlags());
122 return e;
125 bool loadFromXml (const XmlElement& xml)
127 if (xml.hasTagName (getTagName()))
129 position.restoreFromXml (xml, position);
130 loadColourAttributes (xml);
132 text = xml.getStringAttribute ("text", "Hello World");
133 typefaceName = xml.getStringAttribute ("fontname", FontPropertyComponent::defaultFont);
134 font.setHeight ((float) xml.getDoubleAttribute ("fontsize", 15.0));
135 font.setBold (xml.getBoolAttribute ("bold", false));
136 font.setItalic (xml.getBoolAttribute ("italic", false));
137 justification = Justification (xml.getIntAttribute ("justification", Justification::centred));
139 return true;
141 else
143 jassertfalse
144 return false;
148 //==============================================================================
149 const String& getText() const throw() { return text; }
151 class SetTextAction : public PaintElementUndoableAction <PaintElementText>
153 public:
154 SetTextAction (PaintElementText* const element, const String& newText_)
155 : PaintElementUndoableAction <PaintElementText> (element),
156 newText (newText_),
157 oldText (element->getText())
161 bool perform()
163 showCorrectTab();
164 getElement()->setText (newText, false);
165 return true;
168 bool undo()
170 showCorrectTab();
171 getElement()->setText (oldText, false);
172 return true;
175 private:
176 String newText, oldText;
179 void setText (const String& t, const bool undoable)
181 if (t != text)
183 if (undoable)
185 perform (new SetTextAction (this, t),
186 "Change text element text");
188 else
190 text = t;
191 changed();
196 //==============================================================================
197 const Font& getFont() const { return font; }
199 class SetFontAction : public PaintElementUndoableAction <PaintElementText>
201 public:
202 SetFontAction (PaintElementText* const element, const Font& newFont_)
203 : PaintElementUndoableAction <PaintElementText> (element),
204 newFont (newFont_),
205 oldFont (element->getFont())
209 bool perform()
211 showCorrectTab();
212 getElement()->setFont (newFont, false);
213 return true;
216 bool undo()
218 showCorrectTab();
219 getElement()->setFont (oldFont, false);
220 return true;
223 private:
224 Font newFont, oldFont;
227 void setFont (const Font& newFont, const bool undoable)
229 if (font != newFont)
231 if (undoable)
233 perform (new SetFontAction (this, newFont),
234 "Change text element font");
236 else
238 font = newFont;
239 changed();
244 //==============================================================================
245 class SetTypefaceAction : public PaintElementUndoableAction <PaintElementText>
247 public:
248 SetTypefaceAction (PaintElementText* const element, const String& newValue_)
249 : PaintElementUndoableAction <PaintElementText> (element),
250 newValue (newValue_),
251 oldValue (element->getTypefaceName())
255 bool perform()
257 showCorrectTab();
258 getElement()->setTypefaceName (newValue, false);
259 return true;
262 bool undo()
264 showCorrectTab();
265 getElement()->setTypefaceName (oldValue, false);
266 return true;
269 private:
270 String newValue, oldValue;
273 void setTypefaceName (const String& newFontName, const bool undoable)
275 if (undoable)
277 perform (new SetTypefaceAction (this, newFontName),
278 "Change text element typeface");
280 else
282 typefaceName = newFontName;
283 changed();
287 const String getTypefaceName() const throw() { return typefaceName; }
289 //==============================================================================
290 const Justification& getJustification() const throw() { return justification; }
292 class SetJustifyAction : public PaintElementUndoableAction <PaintElementText>
294 public:
295 SetJustifyAction (PaintElementText* const element, const Justification& newValue_)
296 : PaintElementUndoableAction <PaintElementText> (element),
297 newValue (newValue_),
298 oldValue (element->getJustification())
302 bool perform()
304 showCorrectTab();
305 getElement()->setJustification (newValue, false);
306 return true;
309 bool undo()
311 showCorrectTab();
312 getElement()->setJustification (oldValue, false);
313 return true;
316 private:
317 Justification newValue, oldValue;
320 void setJustification (const Justification& j, const bool undoable)
322 if (justification.getFlags() != j.getFlags())
324 if (undoable)
326 perform (new SetJustifyAction (this, j),
327 "Change text element justification");
329 else
331 justification = j;
332 changed();
337 void convertToPath()
339 font = FontPropertyComponent::applyNameToFont (typefaceName, font);
341 const Rectangle<int> r (getCurrentAbsoluteBounds());
343 GlyphArrangement arr;
344 arr.addCurtailedLineOfText (font, text,
345 0.0f, 0.0f, (float) r.getWidth(),
346 true);
348 arr.justifyGlyphs (0, arr.getNumGlyphs(),
349 (float) r.getX(), (float) r.getY(),
350 (float) r.getWidth(), (float) r.getHeight(),
351 justification);
353 Path path;
354 arr.createPath (path);
356 convertToNewPathElement (path);
359 private:
360 String text;
361 Font font;
362 String typefaceName;
363 Justification justification;
365 Array <Justification> justificationTypes;
367 //==============================================================================
368 class TextProperty : public TextPropertyComponent,
369 public ChangeListener
371 public:
372 TextProperty (PaintElementText* const element_)
373 : TextPropertyComponent ("text", 2048, false),
374 element (element_)
376 element->getDocument()->addChangeListener (this);
379 ~TextProperty()
381 element->getDocument()->removeChangeListener (this);
384 void setText (const String& newText) { element->setText (newText, true); }
385 const String getText() const { return element->getText(); }
387 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
389 private:
390 PaintElementText* const element;
393 //==============================================================================
394 class FontNameProperty : public FontPropertyComponent,
395 public ChangeListener
397 public:
398 FontNameProperty (PaintElementText* const element_)
399 : FontPropertyComponent ("font"),
400 element (element_)
402 element->getDocument()->addChangeListener (this);
405 ~FontNameProperty()
407 element->getDocument()->removeChangeListener (this);
410 void setTypefaceName (const String& newFontName) { element->setTypefaceName (newFontName, true); }
411 const String getTypefaceName() const { return element->getTypefaceName(); }
413 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
415 private:
416 PaintElementText* const element;
419 //==============================================================================
420 class FontStyleProperty : public ChoicePropertyComponent,
421 public ChangeListener
423 public:
424 FontStyleProperty (PaintElementText* const element_)
425 : ChoicePropertyComponent ("style"),
426 element (element_)
428 element->getDocument()->addChangeListener (this);
430 choices.add ("normal");
431 choices.add ("bold");
432 choices.add ("italic");
433 choices.add ("bold + italic");
436 ~FontStyleProperty()
438 element->getDocument()->removeChangeListener (this);
441 void setIndex (int newIndex)
443 Font f (element->getFont());
445 f.setBold (newIndex == 1 || newIndex == 3);
446 f.setItalic (newIndex == 2 || newIndex == 3);
448 element->setFont (f, true);
451 int getIndex() const
453 if (element->getFont().isBold() && element->getFont().isItalic())
454 return 3;
455 else if (element->getFont().isBold())
456 return 1;
457 else if (element->getFont().isItalic())
458 return 2;
460 return 0;
463 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
465 private:
466 PaintElementText* const element;
469 //==============================================================================
470 class FontSizeProperty : public SliderPropertyComponent,
471 public ChangeListener
473 public:
474 FontSizeProperty (PaintElementText* const element_)
475 : SliderPropertyComponent ("size", 1.0, 250.0, 0.1, 0.3),
476 element (element_)
478 element->getDocument()->addChangeListener (this);
481 ~FontSizeProperty()
483 element->getDocument()->removeChangeListener (this);
486 void setValue (double newValue)
488 element->getDocument()->getUndoManager().undoCurrentTransactionOnly();
490 Font f (element->getFont());
491 f.setHeight ((float) newValue);
493 element->setFont (f, true);
496 double getValue() const
498 return element->getFont().getHeight();
501 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
503 private:
504 PaintElementText* const element;
507 //==============================================================================
508 class TextJustificationProperty : public JustificationProperty,
509 public ChangeListener
511 public:
512 TextJustificationProperty (PaintElementText* const element_)
513 : JustificationProperty ("layout", false),
514 element (element_)
516 element->getDocument()->addChangeListener (this);
519 ~TextJustificationProperty()
521 element->getDocument()->removeChangeListener (this);
524 void setJustification (const Justification& newJustification)
526 element->setJustification (newJustification, true);
529 const Justification getJustification() const
531 return element->getJustification();
534 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
536 private:
537 PaintElementText* const element;
540 //==============================================================================
541 class TextToPathProperty : public ButtonPropertyComponent
543 public:
544 TextToPathProperty (PaintElementText* const element_)
545 : ButtonPropertyComponent ("path", false),
546 element (element_)
550 void buttonClicked()
552 element->convertToPath();
555 const String getButtonText() const
557 return "convert text to a path";
560 private:
561 PaintElementText* const element;
566 #endif // __JUCER_PAINTELEMENTTEXT_JUCEHEADER__